let token = localStorage.getItem('Token')
let welcome = document.querySelector('#welcome')
let textAvatar = document.querySelectorAll('.text-avatar')
let avatarImg = document.querySelectorAll('.layui-nav-img')
let btnLogout = document.querySelector('#btnLogout')
//console.log(token);

// if(!token){
//   document.body.innerHTML=''
//   alert('请您先登录');
//   location.href = './login.html'
// }
axios({
  method: 'GET',
  url: `/my/userinfo`,
  // headers:{
  //   'Authorization':token

  // },

}).then(res => {
  console.log(res);
  let data = res.data.data
  render(data)
})
btnLogout.addEventListener('click', () => {

  if (confirm('确认要退出么？')) {
    localStorage.removeItem('Token')
    location.href = './login.html'
  }
})
function render(data) {

  let name = data.nickname || data.username
  // welcome.style.display = 'block'
  welcome.textContent = '欢迎' + name

  if (data.user_pic !== null) {

    avatarImg.forEach(item => {
      item.style.opacity = 1
      item.src = data.user_pic

    })
    textAvatar.forEach(item => {
      item.style.opacity = 0
      // item.innerHTML=data.username[0].toUpperCase()
    })

  } else {

    textAvatar.forEach(item => {
      item.style.opacity = 1
      item.innerHTML = data.username[0].toUpperCase()
    })
    avatarImg.forEach(item => {
      item.style.opacity = 0
    })

  }




}